package com.situ.springboot.controller;

import com.situ.springboot.pojo.Banji;
import com.situ.springboot.service.IBanjiService;
import com.situ.springboot.util.JSONResult;
import com.situ.springboot.util.LayUITableResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

// SpringMVC
@Controller
@RequestMapping("/banji")
public class BanjiController {
    // IBanjiService banjiService = new BanjiServiceImpl();
    @Autowired
    private IBanjiService banjiService;

    // http://localhost:8080/banji/selectAll
    @RequestMapping("/selectAll")
    @ResponseBody    // 以json形式返回数据
    public List<Banji> selectAll() {
        // Ctrl+Shift+/
        List<Banji> list  = banjiService.selectAll();
        return list;
    }

    // http://localhost:8080/banji/getBanjiListPage
    @RequestMapping("/getBanjiListPage")
    public String getBanjiListPage(Model model) { // 返回值String代表要转发的界面
        // List<Banji> list  = banjiService.selectAll();
        // model.addAttribute("list", list);
        return "/WEB-INF/banji_list.jsp";

    }

    // /banji/deleteById?id=1
    @RequestMapping("/deleteById")
    @ResponseBody
    public JSONResult deleteById(Integer id) {
        // sout
        System.out.println("id: " + id);
        // Alt+Enter
        banjiService.deleteById(id);

        // 删除完了之后应该，再去完成一次查找所有展示数据的过程
        // return "redirect:/banji/getBanjiListPage";

        return JSONResult.ok("删除成功");
    }

    // http://localhost:8080/banji/selectByPage?page=1&limit=10&id=9&name=zs&age=23
    // page代表：第几页数据   limit:每页有多少条数据
    @RequestMapping("/selectByPage")
    @ResponseBody
    public LayUITableResult selectByPage(Integer page, Integer limit, Banji banji) {
        LayUITableResult layUITableResult = banjiService.selectByPage(page, limit, banji);
        return layUITableResult;
    }

    @RequestMapping("/getBanjiAddPage")
    public String getBanjiAddPage() {
        return "/WEB-INF/banji_add.jsp";
    }

    @RequestMapping("/add")
    @ResponseBody
    public JSONResult add(Banji banji) {
        banjiService.add(banji);
        return JSONResult.ok("添加成功");
    }

    @RequestMapping("/getBanjiUpdatePage")
    public String getBanjiUpdatePage(Integer id, Model model) {
        /*根据id查出要更新的学生信息*/
        Banji banji = banjiService.selectById(id);
        // 把查出来的数据放到Model内存里面，然后转发到banji_update.jsp页面进行展示
        model.addAttribute("banji", banji);
        return "/WEB-INF/banji_update.jsp";
    }

    @RequestMapping("/update")
    @ResponseBody
    public JSONResult update(Banji banji) {
        banjiService.update(banji);
        return JSONResult.ok("更新成功");
    }

    // '10,16'
    @RequestMapping("/deleteAll")
    @ResponseBody
    public JSONResult deleteAll(Integer[] ids) {
        banjiService.deleteAll(ids);
        return JSONResult.ok("删除成功");
    }
}
